package com.stn.zjx.givingtreasure.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stn.zjx.givingtreasure.event.NetWorkEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Created by 朱俊鑫 on 2018
 * 基础抽象UI碎片
 * @author 朱俊鑫
 * @since 2018-3
 * @see Fragment
 * @property mainActivity 碎片的基础UI界面
 * @
 */
public abstract class BaseSearchResultFragment extends Fragment {
    //基础界面
    public BaseActivity activity;
    //数据
    public Bundle data;
    public View rootView;
    /**
     * 当碎片被创建的时候
     * @param savedInstanceState 保存状态
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity= (BaseActivity) getActivity();
        EventBus.getDefault().register(this);
        if(getArguments()!=null){
            data=getArguments();
        }
    }
    /**
     * @param  netWorkEvent 网络状态事件
     * 订阅,监听网络状态事件
     * */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void NetworkEvent(NetWorkEvent netWorkEvent){
        //网络状态回调，主线程
        networkStatus(netWorkEvent);
    }
    /**
     *
     * 回调，网络改变状态
     */
    //网络改变状态
    public  abstract void networkStatus(NetWorkEvent netWorkEvent);
    /**
     *
     * @param inflater 将布局转换成组件
     * @param container  夫类布局
     * @param savedInstanceState 保存状态
     * @return  返回布局组件
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         if(rootView==null){
             rootView =initView(inflater,container);
             initData();
         }
        return rootView;
    }
    /**
     * 当碎片在基础UI界面中创建时
     * @param savedInstanceState 保存状态
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
    //初始化组件
    public abstract View initView(LayoutInflater inflater,ViewGroup container);
    //初始化数据
    public abstract void initData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        //注销监听事件
        EventBus.getDefault().unregister(this);
    }
}
